﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.stage;
using Imaginecup.gameState.menu;
using Imaginecup.factory;
using Microsoft.Xna.Framework.Content;
using Imaginecup.shareData;
using Microsoft.Xna.Framework;
using Imaginecup.util.resourceMan;
using Imaginecup.util;
using Microsoft.Xna.Framework.Graphics;
using imaginecup.util;
using Imaginecup.sprite;

namespace Imaginecup.gameState.water.stageMan
{
    public class WaterStageMan : IStageMan, IStageManController, IResetController, IChooseControl
    {
        private IStage m_Stage;
        private bool m_IsClear;
        private bool m_IsGameOver;
        private IFactory m_StageFactory;

        private StageEnum[] m_StageArray = new StageEnum[]{
            StageEnum.Stage1,
        };

        private Dictionary<StageEnum, ChoiceBox> m_StageEnable;
        private ContentManager m_Content;
        private StageEnum m_CurrentEnum;

        public WaterStageMan(IFactory stageFactory)
        {
            m_StageFactory = stageFactory;
            m_StageEnable = new Dictionary<StageEnum, ChoiceBox>();
            m_Content = new ContentManager(StateShareData.GetShareData().GetGame().Services);
            m_Content.RootDirectory = "Content";
        }


        public void LoadResource()
        {
            Vector2 boxSize = new Vector2(105, 104);
            ResourceMan.GetResourceMan().SetTextureFrame("ChoiceBox", m_Content.Load<Texture2D>("ui\\sel_butt"), boxSize);
            ResourceMan.GetResourceMan().SetTextureFrame("Select_Back", m_Content.Load<Texture2D>("ui\\sel_1"));
            ResourceMan.GetResourceMan().SetTextureFrame("Select_Frame", m_Content.Load<Texture2D>("ui\\sel_2"));
            ResourceMan.GetResourceMan().SetTextureFrame("Exit", m_Content.Load<Texture2D>("ui\\sel_exit"));
            ResourceMan.GetResourceMan().SetTextureFrame("Select_Num", m_Content.Load<Texture2D>("ui\\sel_num"));

            DrawNumber number = new DrawNumber("Select_Num");

            for (int i = 0; i < m_StageArray.Length; i++)
            {
                ChoiceBox box = m_StageEnable[m_StageArray[i]];
                box.SetDrawNumber(number);
            }


            m_IsClear = false;
            m_IsGameOver = false;

            m_Stage = (IStage)m_StageFactory.GetObject(StageEnum.ChoiceStage);
            m_Stage.SetStageMan(this);
        }

        public void LoadStageData()
        {
            InitChoiceBox();
        }

        private void InitChoiceBox()
        {
            Vector2 initPos = new Vector2(112, 133);
            Vector2 boxSize = new Vector2(105, 104);
            Vector2 gab = new Vector2(118, 119);

            int maxX = 1;
            int maxY = 1;

            EntityInfo info = new EntityInfo(initPos, boxSize);


            for (int y = 0; y < maxY; y++)
            {
                info.mPosition.X = initPos.X;

                for (int x = 0; x < maxX; x++)
                {
                    int index = y * maxX + x;

                    ChoiceBox choiceBox = new ChoiceBox(index + 1, false);

                    ChangeSourceSizeSprite sourceSprite = new ChangeSourceSizeSprite();
                    sourceSprite.SetTextureName("ChoiceBox");
                    sourceSprite.SetSourceInfoClass(choiceBox);

                    choiceBox.SetEntityInfo(info);
                    choiceBox.SetSprite(sourceSprite);
                    m_StageEnable.Add(m_StageArray[index], choiceBox);

                    info.mPosition.X += gab.X;

                }
                info.mPosition.Y += gab.Y;
            }

            m_StageEnable[StageEnum.Stage1].SetEnable();
        }

        public void Release()
        {
            m_Stage.Release();
            ClearStageInfo();
            m_Content.Unload();
            //for (int i = 0; i < m_StageArray.Length; i++)
            //{
            //    ChoiceBox choiceBox = m_StageEnable[m_StageArray[i]];
            //    choiceBox.Release();
            //}

            //m_StageEnable.Clear();
            //m_StageArray = null;
        }

        private void ClearStageInfo()
        {
        }

        public IStageLoop GetStage()
        {
            return (IStageLoop)m_Stage;
        }

        public bool IsClear()
        {
            return m_IsClear;
        }

        public bool IsGameOver()
        {
            return m_IsGameOver;
        }

        public void SetClear()
        {
            m_IsClear = true;
        }

        public void SetGameOver()
        {
            m_IsGameOver = true;
        }

        public void ChangeStage(StageEnum stageNum)
        {
            ClearStageInfo();
            m_Stage = (IStage)m_StageFactory.GetObject(stageNum);
            m_Stage.SetStageMan(this);

            m_CurrentEnum = stageNum;
        }

        public StageEnum GetStageEnum(int stageCount)
        {
            return m_StageArray[stageCount];
        }
        public ChoiceBox GetChoiceBox(StageEnum stageEnum)
        {
            return m_StageEnable[stageEnum];
        }

        public bool HasNext(int i)
        {
            if (m_StageArray.Length > i)
                return true;
            return false;
        }


        public void SetEnableChoice(StageEnum stageNum)
        {
            m_StageEnable[stageNum].SetEnable();
        }

        public void Reset()
        {
            ChangeStage(m_CurrentEnum);
        }

        public void SetChooseStage()
        {
            ChangeStage(StageEnum.ChoiceStage);
        }
    }
}
